package kr.hkit.summury_java.erp.serviceUI;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.summury_java.erp.dao.DaoInterface;
import kr.hkit.summury_java.erp.dto.Department;
import kr.hkit.summury_java.erp.dto.Employee;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;

public class PanelEmployeeInputUI extends JPanel implements ActionListener {
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private InputPanel pTitle;
	private InputPanel pManager;
	private InputPanel pSalary;
	private InputPanel pDeptNo;
	
	private JPanel panel_top;
	private JPanel panel_bottom;
	private JButton btnAdd;
	private JPanel pAlram;
	private JLabel lblAlram;
	
	private DaoInterface<Employee> dao;

	/**
	 * Create the panel.
	 */
	public PanelEmployeeInputUI(String btnName, DaoInterface<Employee> dao){
		initialize();
		this.btnAdd.setText(btnName);
		this.dao = dao;
	}	

	public InputPanel getpEmpNo() {
		return pEmpNo;
	}

	public InputPanel getpEmpName() {
		return pEmpName;
	}

	public InputPanel getpTitle() {
		return pTitle;
	}

	public InputPanel getpManager() {
		return pManager;
	}

	public InputPanel getpSalary() {
		return pSalary;
	}

	public InputPanel getpDeptNo() {
		return pDeptNo;
	}

	public JButton getBtnAdd() {
		return btnAdd;
	}

	public DaoInterface<Employee> getDao() {
		return dao;
	}

	public JLabel getLblAlram() {
		return lblAlram;
	}

	public void setLblAlram(JLabel lblAlram) {
		this.lblAlram = lblAlram;
	}

	public void setDao(DaoInterface<Employee> dao) {
		this.dao = dao;
	}
	
	private void initialize() {
		setLayout(new BorderLayout(0, 0));
		
		panel_top = new JPanel();
		add(panel_top);
		panel_top.setLayout(new GridLayout(0, 1, 0, 5));
		
		pAlram = new JPanel();
		panel_top.add(pAlram);
		
		lblAlram = new JLabel("New label");
		lblAlram.setFont(new Font("맑은 고딕", Font.PLAIN, 12));
		pAlram.add(lblAlram);
		
		pEmpNo = new InputPanel("사원번호");
		panel_top.add(pEmpNo);
		
		pEmpName = new InputPanel("사원명");
		panel_top.add(pEmpName);
		
		pTitle = new InputPanel("직급");
		panel_top.add(pTitle);
		
		pManager = new InputPanel("직속상사");
		panel_top.add(pManager);
		
		pSalary = new InputPanel("급여");
		panel_top.add(pSalary);
		
		pDeptNo = new InputPanel("부서번호");
		panel_top.add(pDeptNo);
		
		panel_bottom = new JPanel();
		add(panel_bottom, BorderLayout.SOUTH);
		
		btnAdd = new JButton("추가");
		btnAdd.setBackground(SystemColor.inactiveCaption);
		btnAdd.setFont(new Font("맑은 고딕", Font.BOLD, 12));
		btnAdd.addActionListener(this);
		panel_bottom.add(btnAdd);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnAdd) {
			actionPerformedBtnAdd(e);
		}
	}
	
	private boolean inValidCheck() {
		//공백만 체크
		if(pEmpNo.getTf().getText().equals("")) {
			pEmpNo.getTf().requestFocus();
			return false;
		}
		if(pEmpName.getTf().getText().equals("")) {
			pEmpName.getTf().requestFocus();
			return false;
		}
		return true;
	}
	
	private Employee getInfo() {
		if (inValidCheck()){
			int empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
			String empName = pEmpName.getTf().getText().trim();
			int title = Integer.parseInt(pTitle.getTf().getText().trim());
			int manager = Integer.parseInt(pManager.getTf().getText().trim());
			int salary = Integer.parseInt(pSalary.getTf().getText().trim());
			int dno = Integer.parseInt(pDeptNo.getTf().getText().trim());
			return new Employee(empNo, empName, title, manager, salary, dno);
		} else {
			return null;
		}
		
	}
	
	private void setIniTf() { //텍스트필드 초기화
		pEmpNo.getTf().setText("");
		pEmpName.getTf().setText("");
		pTitle.getTf().setText("");
		pManager.getTf().setText("");
		pSalary.getTf().setText("");
		pDeptNo.getTf().setText("");
		
	
	}
	
	protected void actionPerformedBtnAdd(ActionEvent e) {	
		try {
			if(btnAdd.getText().equals("추가")) {
				Employee emp = getInfo();
				dao.insertItem(emp);
				JOptionPane.showMessageDialog(this, "정상적으로 입력되었습니다.");
				setIniTf();
			}
			
			if(btnAdd.getText().equals("수정")) {
				Employee emp = getInfo();
				dao.updateItemByNo(emp);
				JOptionPane.showMessageDialog(this, "정상적으로 수정되었습니다.");
				setIniTf();
			}
			
			if(btnAdd.getText().equals("삭제")) {
				int empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
				Employee emp = new Employee(empNo);
				dao.deleteItemByNo(emp);
				JOptionPane.showMessageDialog(this, "정상적으로 삭제되었습니다.");
				setIniTf();
			}
			
			if(btnAdd.getText().equals("검색")) {
				int empNo = Integer.parseInt(pEmpNo.getTf().getText().trim());
				Employee emp = new Employee(empNo);
				JOptionPane.showMessageDialog(this, dao.searchItem(emp));
				setIniTf();
			}
			
		} catch(NullPointerException e1) {
			JOptionPane.showMessageDialog(null, "잘못된 정보를 입력하셨습니다.");
		} catch (SQLException e2) {
			JOptionPane.showMessageDialog(this, "입력을 실패하였습니다. 다시 확인하여 주세요.");
		} catch (NumberFormatException e3) {
			JOptionPane.showMessageDialog(this, "정보를 입력해주세요.");
		}
		
	}
}
